const {
  getTodayStep,
  setTodayStep,
} = require("../../../utils/request/Api/user");

Page({
  data: {
    steps: [
      {
        text: "1000",
        id: 1,
      },
      {
        text: "2000",
        id: 2,
      },
      {
        text: "3000",
        id: 3,
      },
      {
        text: "5000",
        id: 4,
      },
      {
        text: "10000",
        id: 5,
      },
      {
        text: "自定义",
        id: 6,
      },
    ],
    active: 2,
  },

  onLoad() {
    getTodayStep().then((res) => {
      let index = this.data.steps.findIndex((item) => item.text == res);
      if (index < 0) {
        index = this.data.steps.length - 1;
      }
      let steps = this.data.steps;
      steps[index].text = res;
      // index = 1
      this.setData({
        active: index,
        steps: steps,
      });
    });
  },

  submit(e) {
    // const index = e.target.dataset.index;
    // const text = this.data.steps[index].text;

    const id = e.target.dataset.id;
    const index = this.data.steps.findIndex((item) => item.id == id);
    const text = this.data.steps[index].text;

    this.setData({
      active: index,
    });

    setTodayStep({
      goal: text,
    }).then((res) => {});
  },
});
